package com.herui.ota.service.iml;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.herui.ota.dao.mysql.DeviceOtaConfigMapper;
import com.herui.ota.entity.DeviceOta;
import com.herui.ota.entity.DeviceOtaConfig;
import com.herui.ota.enums.ResponseEnum;
import com.herui.ota.exception.ServiceException;
import com.herui.ota.pojo.dto.DeviceOtaSeriesChannel;
import com.herui.ota.pojo.form.DeviceOtaConfigFilterForm;
import com.herui.ota.pojo.form.OtaSoftUpdateForm;
import com.herui.ota.service.DeviceOtaConfigService;
import com.herui.ota.service.DeviceOtaService;
import com.mlnx.common.form.PageForm;
import org.apache.commons.lang3.StringUtils;
import org.shan.base.layer.user.pojo.vo.RoleVo;
import org.shan.base.layer.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * author name: amanda.shan
 * create time: 2024-04-09 16:55:12
 */
@Service
public class DeviceOtaConfigServiceIml extends BaseService<DeviceOtaConfigMapper, DeviceOtaConfig> implements DeviceOtaConfigService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private DeviceOtaService deviceOtaService;

    @Autowired
    @Override
    public void setBaseMapper(DeviceOtaConfigMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Override
    public IPage page(PageForm pageForm, Object filter) throws IllegalAccessException {

        Page page = baseMapper.listDeviceOtaConfigVoPage(new Page(pageForm.getCurrent(), pageForm.getSize()),
                (DeviceOtaConfigFilterForm) filter);
        return page;
    }

    @Override
    public void save(DeviceOtaConfig entity) {

        checkRight("soft_rightotaconfig");

        if (checkOtaSame(entity)){
            throw new ServiceException(ResponseEnum.OTA_SAME);
        }

        super.save(entity);
    }

    @Override
    public boolean updateById(DeviceOtaConfig entity) {
        checkRight("soft_rightotaconfig");

        if (entity.getOtaId() != null && checkOtaSame(entity)){
            throw new ServiceException(ResponseEnum.OTA_SAME);
        }

        if (!StringUtils.isEmpty(entity.getState()) && "1".equals(entity.getState())) {
            return channelOpen(entity);
        }

        return super.updateById(entity);
    }

    @Override
    public void channelOpen(List<DeviceOtaConfig> deviceOtaConfigs) {
        deviceOtaConfigs.stream().forEach(deviceOtaConfig -> {
            if (!deviceOtaConfig.getState().equals("1")) {
                channelOpen(deviceOtaConfig);
            }
        });

    }

    @Override
    public boolean removeById(Serializable id) {
        checkRight("soft_rightotaconfig");
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        checkRight("soft_rightotaconfig");
        return super.removeByIds(idList);
    }

    @Transactional
    @Override
    public void changeOtaSoftId(OtaSoftUpdateForm otaSoftUpdateForm) {
        checkRight("soft_rightotaconfig");

        Integer softId = otaSoftUpdateForm.getSoftId();
        List<Integer> otaConfigIds = otaSoftUpdateForm.getOtaConfigIds();

        DeviceOtaConfig deviceOtaConfig = new DeviceOtaConfig();
        deviceOtaConfig.setSoftId(softId);

        for (int i = 0; i < otaConfigIds.size(); i++) {
            Integer id = otaConfigIds.get(i);
            deviceOtaConfig.setId(id);
            super.updateById(wrapperEditor(deviceOtaConfig));
        }
    }

    private boolean channelOpen(DeviceOtaConfig deviceOtaConfig) {
        deviceOtaConfig.setState("1");
        DeviceOta deviceOta = deviceOtaService.getById(deviceOtaConfig.getOtaId());
        String state = deviceOta.getState();
        if (state.equals("TEST")) {
            throw new ServiceException(ResponseEnum.OTA_FAIL.getCode(), "固件还在测试中无法打开");
        } else if (state.equals("ARCHIVED")) {
            deviceOtaService.otaOnline(deviceOta.getId());
        }

        return super.updateById(deviceOtaConfig);
    }

    /**
     * 检查系列和渠道是否重复
     *
     * @param deviceOtaConfig
     */
    private boolean checkOtaSame(DeviceOtaConfig deviceOtaConfig) {
        DeviceOta deviceOta = deviceOtaService.getById(deviceOtaConfig.getOtaId());
        DeviceOtaSeriesChannel deviceOtaSeriesChannel = baseMapper.findDeviceOtaSeriesChannel(deviceOta.getSeries(),
                deviceOtaConfig.getChannel());
        return deviceOtaSeriesChannel != null && !deviceOtaConfig.getId().equals(deviceOtaSeriesChannel.getId());
    }
}

